package com.hjm.project.incomeInfo.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.ExceptionUtil;
import com.hjm.common.utils.StringUtils;
import com.hjm.common.utils.excel.OutbbExcel;
import com.hjm.common.utils.file.FileUtils;
import com.hjm.common.utils.security.ShiroUtils;
import com.hjm.project.job.task.domain.JobTask;
import com.hjm.project.terminal.manufacturer.domain.Manufacturer;
import com.hjm.project.terminal.manufacturer.mapper.ManufacturerMapper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.hjm.project.incomeInfo.mapper.IncomeInfoMapper;
import com.hjm.project.incomeInfo.domain.IncomeInfo;
import com.hjm.project.incomeInfo.service.IIncomeInfoService;
import com.hjm.common.utils.text.Convert;

import javax.annotation.Resource;

/**
 * 收益统计Service业务层处理
 * 
 * @author maker-platform
 * @date 2024-03-13
 */
@Service
public class IncomeInfoServiceImpl implements IIncomeInfoService {
    @Resource
    private IncomeInfoMapper incomeInfoMapper;
    @Resource
    private ManufacturerMapper manufacturerMapper;

    /**
     * 查询收益统计
     * 
     * @param id 收益统计ID
     * @return 收益统计
     */
    @Override
    public IncomeInfo selectIncomeInfoById(Integer id) {
        return incomeInfoMapper.selectIncomeInfoById(id);
    }

    /**
     * 查询收益统计列表
     * 
     * @param incomeInfo 收益统计
     * @return 收益统计
     */
    @Override
    public List<IncomeInfo> selectIncomeInfoList(IncomeInfo incomeInfo) {
        return incomeInfoMapper.selectIncomeInfoList(incomeInfo);
    }

    /**
     * 查询收益统计列表
     *
     * @param incomeInfo 收益统计
     * @return 收益统计 一个结果
     */
    @Override
    public IncomeInfo selectIncomeInfoOne(IncomeInfo incomeInfo) {
        return incomeInfoMapper.selectIncomeInfoOne(incomeInfo);
    }

    /**
     * 新增收益统计
     * 
     * @param incomeInfo 收益统计
     * @return 结果
     */
    @Override
    public int insertIncomeInfo(IncomeInfo incomeInfo) {
        return incomeInfoMapper.insertIncomeInfo(incomeInfo);
    }

    /**
     * 修改收益统计
     * 
     * @param incomeInfo 收益统计
     * @return 结果
     */
    @Override
    public int updateIncomeInfo(IncomeInfo incomeInfo) {
        return incomeInfoMapper.updateIncomeInfo(incomeInfo);
    }

    @Override
    public int deleteIncomeInfo(IncomeInfo incomeInfo) {
        return incomeInfoMapper.deleteIncomeInfo(incomeInfo);
    }

    /**
     * 删除收益统计对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteIncomeInfoByIds(String ids) {
        return incomeInfoMapper.deleteIncomeInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除收益统计信息
     * 
     * @param id 收益统计ID
     * @return 结果
     */
    @Override
    public int deleteIncomeInfoById(Integer id) {
        return incomeInfoMapper.deleteIncomeInfoById(id);
    }

    @Override
    @Async
    public void selectCount(String userName,String tradeDate,List<Manufacturer> manufacturers) {
        for (Manufacturer manufacturer : manufacturers) {
            String manuNum = manufacturer.getManuNum().toString();
            try {
                IncomeInfo incomeInfo = incomeInfoMapper.selectCount(tradeDate,manuNum);
                IncomeInfo service = incomeInfoMapper.selectSerivce(tradeDate, manuNum);
                String day = DateUtils.getDay("yyyy-MM-dd", 1, tradeDate);
                IncomeInfo ycFr = incomeInfoMapper.selectYcFr(day, manuNum);
                IncomeInfo ycAward = incomeInfoMapper.selectYcAward(day, manuNum);
                IncomeInfo sjTx = incomeInfoMapper.selectSjTx(tradeDate);
                IncomeInfo info = incomeInfoMapper.selectCdAmount(tradeDate);
                incomeInfo.setTradeDate(tradeDate);
                incomeInfo.setManuNum(manufacturer.getManuNum().toString());
                incomeInfo.setStatus("1");
                incomeInfo.setLlfAmount(service.getLlfAmount());
                incomeInfo.setFwfAmount(service.getFwfAmount());
                incomeInfo.setYcProfit(ycFr.getYcProfit());
                incomeInfo.setYcAward(ycAward.getYcAward());
                incomeInfo.setSjAmount(sjTx.getSjAmount());
                incomeInfo.setCdAmount(info == null ? new BigDecimal(String.valueOf(BigDecimal.ZERO)) : StringUtils.isBlank(info.getCdAmount().toString()) ? new BigDecimal(String.valueOf(BigDecimal.ZERO)) : info.getCdAmount());
                incomeInfo.setNote(manufacturer.getManuName() + "报表生成完成");
                incomeInfoMapper.updateIncomeInfo(incomeInfo);
            } catch (Exception e) {
                IncomeInfo incomeInfo = new IncomeInfo();
                incomeInfo.setTradeDate(tradeDate);
                incomeInfo.setStatus("2");
                incomeInfo.setNote(manufacturer.getManuName() + "报表生成失败");
                incomeInfo.setManuNum(manuNum);
                incomeInfoMapper.updateIncomeInfo(incomeInfo);
                e.printStackTrace();
            }
        }
    }

    /**
     * 新增商户虚户政策
     * @return 结果
     */
    @Override
    @Async("threadPoolTaskExecutor")
    public CompletableFuture<JobTask> exportBb(IncomeInfo incomeInfo) {
        String fileName = FileUtils.getFileName("收益报表");
        JobTask jobTask = new JobTask();
        jobTask.setOperatorType(Long.parseLong("21"));
        jobTask.setOperatorContent("导入成功");
        jobTask.setCreateBy(ShiroUtils.getLoginName());
        jobTask.setFileName(fileName);
        try{
            String[] title = {
                    "交易日期","品牌名称","借记卡交易总额","借记卡分润总额",
                    "贷记卡交易总额","贷记卡分润总额","扫码交易总额",
                    "扫码分润总额","流量费总额","服务费总额","已出分润","已出奖励","实付支出(当天提现)","沉淀金额"};
            List<String[]> list = new ArrayList<>();
            List<IncomeInfo> incomeInfos = incomeInfoMapper.selectIncomeInfoList(incomeInfo);
            for (IncomeInfo info : incomeInfos) {
                String[] str = new String[14];
                str[0] = info.getTradeDate();
                str[1] = info.getManuName();
                str[2] = info.getJjkAmount().toString();
                str[3] = info.getJjkProfit().toString();
                str[4] = info.getDjkAmount().toString();
                str[5] = info.getDjkProfit().toString();
                str[6] = info.getSmAmount().toString();
                str[7] = info.getSmProfit().toString();
                str[8] = info.getLlfAmount().toString();
                str[9] = info.getFwfAmount().toString();
                str[10] = info.getYcProfit().toString();
                str[11] = info.getYcAward().toString();
                str[12] = info.getSjAmount().toString();
                str[13] = info.getCdAmount().toString();
                list.add(str);
            }
            OutbbExcel.outExcel(FileUtils.getAbsoluteFile(),fileName,"收益报表",title,list);
            jobTask.setOperatorContent("报表处理成功");
        }catch (Exception e){
            jobTask.setOperatorContent("报表处理异常:" + ExceptionUtil.getExceptionMessage(e).substring(0,150));
            e.printStackTrace();
        }
        return CompletableFuture.completedFuture(jobTask);
    }

}
